﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Utility
{
    public static class RichText
    {
        public static void WriteLog(this RichTextBox richTextBox, string str, Color color)
        {
            lock ("richTextBox")
            {
                richTextBox.SelectionColor = Color.Black;
                richTextBox.AppendText(DateTime.Now.ToString("hh:mm:ss.fff"));
                richTextBox.SelectionColor = color;
                richTextBox.AppendText(string.Format(" : {0} \n", str));
                ScrollToEnd(richTextBox);
            }

        }
        public static void WriteLog(this RichTextBox richTextBox, string str)
        {
            lock ("richTextBox")
            {
                richTextBox.SelectionColor = Color.Black;
                richTextBox.AppendText(DateTime.Now.ToString("hh:mm:ss.fff"));
                richTextBox.AppendText(string.Format(" : {0} \n", str));
                ScrollToEnd(richTextBox);
            }
        }

        public static void Write(this RichTextBox richTextBox, string str, Color color, bool bold = false)
        {
            lock ("richTextBox")
            {
                richTextBox.Font = new Font(richTextBox.Font.FontFamily, richTextBox.Font.Size,
                                            bold ? FontStyle.Bold : FontStyle.Regular);
                richTextBox.SelectionColor = color;
                richTextBox.AppendText(string.Format("{0}", str));
                ScrollToEnd(richTextBox);
            }
        }

        public static void WriteLine(this RichTextBox richTextBox, string str, Color color)
        {
            lock ("richTextBox")
            {
                richTextBox.SelectionColor = color;
                richTextBox.AppendText(string.Format("{0}\n", str));
                ScrollToEnd(richTextBox);
            }
        }
        public static void ScrollToEnd(RichTextBox richTextBox)
        {
            richTextBox.SelectionStart = richTextBox.Text.Length;
            richTextBox.ScrollToCaret();
        }
    }
}
